package cn.ll.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ll.po.Permissions;
import cn.ll.service.ItemService;
import cn.ll.service.PermissionsService;

public class PermissionsAction extends Action {
	
	
	public void save(HttpServletRequest request,HttpServletResponse response){
		String name = request.getParameter("name");
		String action = request.getParameter("action");
		String scope = request.getParameter("scope");
		
		if(name != null && name.length() > 0 && action != null && action.length() > 0 && scope != null && scope.length() > 0){
			Permissions	permissions = new Permissions();
			PermissionsService permissionsService = (PermissionsService) this.getApplicationContext().getBean("permissionsService");
			permissions.setName(name);
			permissions.setAction(action);
			permissions.setScope(scope);
			permissionsService.save(permissions);
			
			Map map = new HashMap(0);
			map.put("success", true);
			rtn(map, request, response);
			return;
		}
		
		Map map = new HashMap(0);
		map.put("success", false);
		rtn(map, request, response);
		return;
	}
	
	public void update(HttpServletRequest request,HttpServletResponse response){
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String action = request.getParameter("action");
		String scope = request.getParameter("scope");
		if(id != null && id.length() > 0){
			PermissionsService permissionsService = (PermissionsService) this.getApplicationContext().getBean("permissionsService");
			Permissions permissions = permissionsService.findById(Integer.parseInt(id));
			if(permissions != null){
				if(name != null && name.length() > 0){
					permissions.setName(name);
				}
				if(action != null && action.length() > 0){
					permissions.setAction(action);
				}
				if(scope != null && scope.length() > 0){
					permissions.setScope(scope);
				}
				
				permissionsService.update(permissions);
				
				Map map = new HashMap(0);
				map.put("success", true);
				rtn(map, request, response);
				return;
			}
		}
		
		Map map = new HashMap(0);
		map.put("success", false);
		rtn(map, request, response);
		return;
	}
	
	
	
}
